package com.cby.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cby.order.entity.CbyOrder;
import com.cby.order.entity.query.CbyOrderQuery;
import com.cby.order.entity.vo.CbyOrderVo;
import com.cby.order.mapper.CbyOrderMapper;
import com.cby.order.service.CbyOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cby.other.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wskh
 * @since 2021-11-04
 */
@Service
public class CbyOrderServiceImpl extends ServiceImpl<CbyOrderMapper, CbyOrder> implements CbyOrderService {

    @Autowired
    CbyOrderService orderService;

    @Override
    public R getPlotData() {
        List<Date> xdata = new ArrayList<>();
        List<Integer> ydata = new ArrayList<>();
        List<CbyOrder> orderList = orderService.list();
        for (CbyOrder order : orderList) {
            Date insertTime = order.getInsertTime();
            boolean b = false;
            for (int i = 0; i < xdata.size(); i++) {
                if(xdata.get(i).getTime()==order.getInsertTime().getTime()){
                    b = true;
                    ydata.set(i,ydata.get(i)+order.getBuyNumber());
                    i = xdata.size();
                }
            }
            if(!b){
                xdata.add(new Date(insertTime.getTime()));
                ydata.add(order.getBuyNumber());
            }
        }
        // 获取最大的ydata
        int maxY = Integer.MIN_VALUE;
        for (Integer integer : ydata) {
            if(integer>maxY){
                maxY = integer;
            }
        }
        return R.ok().data("xdata",xdata).data("ydata",ydata).data("max",maxY).data("min",0);
    }

    @Override
    public R queryOrder(long current, long limit, CbyOrderQuery cbyOrderQuery) {

        // 创建page对象
        Page<CbyOrder> orderPage = new Page<>(current,limit);
        // 构建条件
        QueryWrapper<CbyOrder> queryWrapper = new QueryWrapper<>();
//        String name = cbyUserQuery.getName();

        // 按照添加时间排序
        queryWrapper.orderByDesc("insert_time");
//        if (!StringUtils.isEmpty(name)) {
//            queryWrapper.like("name", name);
//        }
        // 调用方法实现 分页条件查询
        IPage<CbyOrder> page = orderService.page(orderPage, queryWrapper);
        List<CbyOrder> records = page.getRecords();

        List<CbyOrderVo> orderVoList = new ArrayList<>();
        for (CbyOrder order : records) {
            CbyOrderVo orderVo = new CbyOrderVo();
            BeanUtils.copyProperties(order,orderVo);
            orderVoList.add(orderVo);
        }
        return R.ok().data("total",page.getTotal()).data("rows",orderVoList);
    }
}
